/**
 * Created by zzy on 2016/12/8.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
var $alert = require("libs/alert");
var $confirm = require("libs/confirm");
var isSale = require('libs/user-type').isSale;
$(function () {
    $('#ft').show();
    var status = {
        draft: '未提交',
        commit: '已提交',
        c_collect: '已揽收',
        c_receive: '已收货',
        c_ourtoom: '集货出库',
        p_inroom: '转运入库',
        p_outroom: '转运出库',
        o_inroom: '海外入库',
        o_shelve: '海外上架',
        cancelled: '报废'
    };
    $('#search').on('click',function () {
        var formData = $('#searchForm').serializeObject();
        formData.starDate = $('#startDt').data('utc0');
        formData.endDate = $('#endDt').data('utc0');
        $('#inOrderTable').datagrid('load',formData);
    });
    $(document).on('click','a[name=orderDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'入库单详情',true,true);
    });
    $(document).on('click','a[name=entDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'客户公司',true,true);
    });
    //订单包裹详情导出
    $(document).on("click","a[name=exportBoxGoods]",function(){
        window.location.href= "/mms/api/v1/p/mms/order/export?id="+$(this).attr('ref');
    });
    //头程自理空运修改国际物流信息
    $(document).on("click","a[name=updateselfOrderinfo]",function(){
        var id = $(this).data('id');
        var carrier = $(this).data('carrier');
        var logisticsno = $(this).data('logisticsno');
        $("#selfOrderUpdate").modal("show");
        $("input[name='orderId']").val(id);
        $("input[name='internationalLogisticsCarrier']").val(carrier);
        $("input[name='internationalLogisticsNo']").val(logisticsno);
    });
    $('#modify').on("click",function (e) {
        var id =  $("input[name='orderId']").val();
        var carrier =  $("input[name='internationalLogisticsCarrier']").val();
        var logisticsno =  $("input[name='internationalLogisticsNo']").val();
        $confirm("确认修改吗?",function () {
            $.ajax({
                url:"api/v1/p/mms/order/updateLogisticsInfo",
                type:"post",
                data:JSON.stringify({"id":id, "internationalLogisticsCarrier":carrier,"internationalLogisticsNo":logisticsno}),
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (result) {
                    if(result.code=="200") {
                        $("#selfOrderUpdate").modal("hide");
                        $("#inOrderTable").datagrid("reload");
                        $alert("修改成功!");
                    }else{
                        $alert(result.message);
                    }
                }
            });
        });

    });
    warehouseInit();
    tableInit();
    function warehouseInit() {
        $('select[name=collectWarehouseId]').combobox({
            url:'api/v1/p/crm/warehouse/query',
            valueField:'warehouseId',
            textField:'warehouseName',
            queryParams:{
                warehouseType:'INSJ'
            },
            loadFilter:function(result){
                if(result.code=='200'){
                    result.data.unshift({warehouseId:'',warehouseName:'全部'});
                    return result.data;
                }
            }
        });
        $('select[name=toWarehouseId]').combobox({
            url:'api/v1/p/crm/warehouse/query',
            valueField:'warehouseId',
            textField:'warehouseName',
            queryParams:{
                warehouseType:'DEST'
            },
            loadFilter:function(result){
                if(result.code=='200'){
                    result.data.unshift({warehouseId:'',warehouseName:'全部'});
                    return result.data;
                }
            }
        })
    }
    function tableInit() {
        $('#inOrderTable').datagrid({
            url:'api/v1/p/mms/order/list',
            border : false,
            height:$(window).height(),
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50,  100 ],
            emptyMsg : "暂无数据",
            singleSelect: true,
            rownumbers:true,
            columns:[[
                {
                    field:'orderNo',
                    title:'入库单号',
                    width:'12%',
                    formatter:function (value,row,index) {
                        var self = 'N';
                        if (row.type == 'S'){
                            self = 'Y';
                        }
                        return "<a class='ellipsis' href='javascript:;' ref='in-order-detail?id="+row.id+"&self="+self+"' name='orderDetail' title='"+value+"'>"+value+"</a>";
                    }
                },{
                    field:'entName',
                    title:'客户名称',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' ref='company-detail?id="+row.entId+"' name='entDetail' title='"+value+"'>"+value+"</a>";
                    }
                },
                {
                    field:'collectWarehouseName',
                    title:'验货仓',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'toWarehouseName',
                    title:'海外仓',
                    width:'14%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'logisticsName',
                    title:'物流产品',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'tNums',
                    title:'包裹数量',
                    width:'6%'
                },{
                    field:'tGoodsNums',
                    title:'商品数量',
                    width:'6%'
                },{
                    field:'skuNums',
                    title:'SKU数',
                    width:'6%'
                },{
                    field:'orderStatus',
                    title:'状态',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return status[value];
                    }
                },{
                    field:'createDt',
                    title:'提交时间',
                    width:'13%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'opertation',
                    title:'操作',
                    hidden:isSale,
                    width:'8%',
                    formatter: function (value, row, index) {
                        var html = "";
                        var express = "SCA" == row.orderType && $.inArray(row.orderStatus, ['draft', 'commit']) != -1;
                        if (express) {
                            html += "<a href='javascript:;' name='updateselfOrderinfo' data-id='"+row.id+"' " +
                                " data-carrier='"+row.internationalLogisticsCarrier+"' " +
                                "  data-logisticsno='"+row.internationalLogisticsNo+"' >修改物流信息</a><br>";
                        }
                        html += "<a href='javascript:;' name='exportBoxGoods'  ref='" + row.id + "'  >包裹详情导出</a>";
                        return html;
                    }
                }
            ]],
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            },
            toolbar:"#tb"
        });
    }
});
